package com.icourt.learn.steam;

import com.alibaba.fastjson.JSON;
import com.icourt.learn.entity.StuDo;



import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toSet;

@Slf4j
public class ToMapSet {


    @Test
    public void test() {

        List<StuDo> list = new ArrayList<>();
        StuDo bo = new StuDo( "zhangsan", 18 );
        list.add( bo );
        bo = new StuDo( "zhangsan1", 19 );
        list.add( bo );
        bo = new StuDo( "lisi", 18 );
        list.add( bo );
        bo = new StuDo( "lisi1", 19 );
        list.add( bo );



        Map<Integer,Set<String>> map=list.stream().collect( groupingBy(StuDo::getAge,mapping(StuDo::getName,toSet())) );
        String str=JSON.toJSONString( map );

        log.debug( "结果{}",str );
    }
}
